package com.dev.common.base.controller;

import com.dev.common.constant.Constant;
import com.github.pagehelper.Page;
import com.google.gson.Gson;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;

/**
 * Created by Administrator on 2016/9/2.
 */
public class BaseController {
    /**
     * 日志记录
     */
    protected Logger logger = LoggerFactory.getLogger(this.getClass());

    /**
     * 进入主页
     *
     * @return
     */
    @RequestMapping(value = {"/index", "/"})
    public ModelAndView init() {
        logger.info("系统初始化");
        return new ModelAndView("index");
    }

    /**
     * 输出字符
     *
     * @param response
     * @param str
     * @throws IOException
     */
    protected void outString(HttpServletResponse response, String str)
            throws IOException {
        response.setContentType("text/html;charset=" + Constant.ENCODE_UTF8);
        response.setCharacterEncoding(Constant.ENCODE_UTF8);
        PrintWriter out = response.getWriter();
        out.print(str);
        out.flush();
        out.close();
    }

    /**
     * 没有权限访问
     *
     * @return
     */
    @RequestMapping(value = {"/unauthor"})
    public ModelAndView unauthor() {
        logger.info("没有权限访问");
        return new ModelAndView("common/error/unauthor");
    }

    /**
     * 跳转错误页面
     *
     * @return
     */
    @RequestMapping(value = {"/error"})
    public ModelAndView error() {
        logger.info("跳转错误页面");
        return new ModelAndView("common/error/error");
    }

    @RequestMapping("empty")
    public String empty() {
        logger.info("跳转示例空白页面");
        return "empty_page";
    }

    /**
     * 返回datagrid 所需的json
     *
     * @param response
     * @param page
     * @throws IOException
     */
    protected void dataGrid(HttpServletResponse response, Page page)
            throws IOException {
        String jsonStr = "";
        logger.info("分页信息{}", page);
        HashMap<String, Object> map = new HashMap<String, Object>();

        map.put("page", page.getTotal());
        map.put("rows", page.getResult());
        logger.info("结果信息{}", map);
        jsonStr = new Gson().toJson(map);

        this.outString(response, jsonStr);
    }

    /**
     * 返回datagrid 所需的json
     *
     * @param response
     * @param page
     * @throws IOException
     */
    protected void bootstrapDataTable(HttpServletResponse response, Page page)
            throws IOException {
        String jsonStr = "";
        HashMap<String, Object> map = new HashMap<String, Object>();

        map.put("rows", page.getResult());
        map.put("total", page.getTotal());
        logger.info("结果信息", map);
        jsonStr = new Gson().toJson(map);
        logger.info("分页信息", jsonStr);
        this.outString(response, jsonStr);
    }
}
